package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 查询所有敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //校验参数合法性
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }
        //如何存在根据name模糊查询
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (dto.getName() != null ){
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        //初始化分页
        dto.checkParam();
        //设置排序
        wrapper.orderByAsc(AdSensitive::getCreatedTime);
        //设置分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        IPage p = this.page(page, wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), page.getTotal(), p.getRecords());
        //数据响应
        return result;
    }

    /**
     * 根据id修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //数据校验
        if (adSensitive.getId() == null || adSensitive.getSensitives() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }
        //根据id获取指定的敏感词
        AdSensitive as = this.getById(adSensitive.getId());
        //检验数据是否存在
        if (as == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据不存在");
        }
        //判断数据是否不一致,为true修改
        if (!adSensitive.getSensitives().equals(as.getSensitives())){
            //判断修改后的数据是否存在
            LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
            if (count(wrapper.like(AdSensitive::getSensitives, adSensitive.getSensitives())) > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
            }
            //修改数据
            this.updateById(adSensitive);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"意料之外的错误");
    }

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //数据校验
        if (adSensitive.getSensitives() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }

        //判断数据是否存在
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (this.count(wrapper.like(AdSensitive::getSensitives,adSensitive.getSensitives())) > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }
        //添加数据
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据Id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //数据校验
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }
        //查询数据是否存在
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (this.count(wrapper.eq(AdSensitive::getId,id)) == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"数据不存在");
        }
        //根据id删除
        this.removeById(id);
        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}